package com.bsj.media.tool;

import com.bsj.consumerQ.until.ExceptionUntil;
import com.bsj.media.tool.dto.SendData;
import com.bsj.media.tool.dto.SendFDDataInfo;
import com.bsj.power.common.def.protocol.rtp.RTPConstant;
import com.bsj.power.common.def.protocol.rtp.RTPInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedList;
import java.util.List;

@Slf4j
public class ComboDataTool {

    private List<RTPInfo> rtpInfoList = new LinkedList<>();

    private boolean isPush = false;

    //0音视频，1视频，2音频
    private int dataType = 0;

    //上一次时间戳
    private long lastTime = 0;

    private byte[] getAllData() {
        if (rtpInfoList == null || rtpInfoList.isEmpty()) {
            return null;
        }
        int nowLen = 0;
        for (RTPInfo rtpInfo : rtpInfoList) {
            byte[] data = rtpInfo.getData();
            nowLen = nowLen + data.length;
        }
        //组合h265数据
        byte[] bytes = new byte[nowLen];
        int offset = 0;
//        int index = 0;
        for (RTPInfo rtpInfo : rtpInfoList) {
            byte[] data = rtpInfo.getData();
            System.arraycopy(data, 0, bytes, offset, data.length);
            offset = offset + data.length;
        }
        return bytes;
    }

    //组合数据
    public SendData pushCombination(RTPInfo rtpInfo) {
        if (rtpInfo == null) {
            return null;
        }
        SendData sendData = null;
        try {
            int dataTypeNow = rtpInfo.getDataType();
            int pageMark = rtpInfo.getPageMark();
            //非透传数据
            if (dataTypeNow != RTPConstant.TRANSMISSION) {
                //原子包
                if (pageMark == 0) {
                    sendData = new SendFDDataInfo();
                    sendData.setData(rtpInfo.getData());
                    sendData.setDataType(dataTypeNow);
                    sendData.setPayLoad(rtpInfo.getPayLoad());
                    sendData.setChannelNo(rtpInfo.getChannelNo());
                    sendData.setPageNo(rtpInfo.getSn());
                    sendData.setNRate(rtpInfo.getNRate());
                    long time = rtpInfo.getTime();
                    sendData.setTime(time);
                    long nowTime = time - lastTime;
                    sendData.setIntervalTime(nowTime);
                    return sendData;
                }
                //如果是第一包
                if (pageMark == RTPConstant.PAGE_FIRST) {
                    isPush = true;
                    //清理掉所有的数据
                    rtpInfoList.clear();
                    rtpInfoList.add(rtpInfo);
                } else {
                    if (isPush) {
                        //中间包
                        if (pageMark == RTPConstant.PAGE_MIDDLE) {
                            rtpInfoList.add(rtpInfo);
                            //如果是最后一包
                        } else if (pageMark == RTPConstant.PAGE_LAST) {
                            rtpInfoList.add(rtpInfo);
                            sendData = new SendFDDataInfo();
                            byte[] allData = getAllData();
                            if (allData != null) {
                                sendData.setData(allData);
                                sendData.setDataType(dataTypeNow);
                                sendData.setPayLoad(rtpInfo.getPayLoad());
                                sendData.setChannelNo(rtpInfo.getChannelNo());
                                sendData.setPageNo(rtpInfo.getSn());
                                sendData.setNRate(rtpInfo.getNRate());
                                sendData.setTime(rtpInfo.getTime());
                                //时间戳
                                long time = rtpInfo.getTime();
                                if (lastTime == 0) {
                                    sendData.setIntervalTime(0);
                                } else {
                                    long nowTime = time - lastTime;
                                    sendData.setIntervalTime(nowTime);
                                }
                                lastTime = time;
                            }
                        } else {
                            log.info("@@未进入到此方法");
                        }
                    } else {
                        log.info("第一包数据未找到:");
                    }
                }
                if (sendData != null) {
                    rtpInfoList.clear();
                    isPush = false;
                }
            } else {
                log.info("透析数据");
            }
        } catch (Exception e) {
            log.error("获取出错:" + ExceptionUntil.getErrorStr(e));
            return null;
        }
        return sendData;
    }

    public void clear() {
        rtpInfoList.clear();
        isPush = false;
    }

    /**
     * 组织GB28181 FD数据
     * @param rtpInfo
     * @return
     */
    public SendData pushAssemble(RTPInfo rtpInfo) {
        SendData sendData = new SendFDDataInfo();
        sendData.setData(rtpInfo.getData());
        sendData.setChannelNo(rtpInfo.getChannelNo());
        sendData.setDataType(rtpInfo.getDataType());
        sendData.setNRate(rtpInfo.getNRate());
        sendData.setPayLoad(rtpInfo.getPayLoad());
        sendData.setTime(rtpInfo.getTime());
        sendData.setPageNo(0);
        return sendData;
    }

}
